//
//  MANotificationDetailVC.m
//  meiang_edu
//
//  Created by Apple on 2018/10/18.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MANotificationDetailVC.h"

@interface MANotificationDetailVC ()
@property(nonatomic,strong) UIImageView *leftIcon;
@property(nonatomic,strong) UIView *notificationView;
@property(nonatomic,strong) UILabel * titleLabel;
@property(nonatomic,strong) UILabel *senderLabel;
@property(nonatomic,strong) UILabel *contentLabel;
@property(nonatomic,strong) UILabel *timeLabel;
@property(nonatomic,strong) UIImageView *notificationImageView;
@end

@implementation MANotificationDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"通知详情";
    [self.leftIcon addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(leftBtnDidClick)]];
    [self.leftIcon setImage:[UIImage imageNamed:@"back"]];
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 7.0, 15, 25.0)];
    [leftView addSubview:self.leftIcon];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftView];
    self.view.backgroundColor=[UIColor colorWithRed:0.922 green:0.922 blue:0.922 alpha:1.00];
    [self initWithView];

}

-(void)initWithView{
    self.notificationView=[[UIView alloc] initWithFrame:CGRectMake(6, 74, kScreenWidth-12, 300)];
    self.notificationView.backgroundColor=[UIColor whiteColor];
    self.notificationView.layer.masksToBounds=YES;
    self.notificationView.layer.cornerRadius=10;
    [self.view addSubview:self.notificationView];
    //标题
    self.titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(6, 6, kScreenWidth-12, 20)];
    self.titleLabel.textColor=[UIColor colorWithRed:0.200 green:0.200 blue:0.200 alpha:1.00];
    self.titleLabel.font=[UIFont boldSystemFontOfSize:18];
    self.titleLabel.text=@"通知";
    [self.notificationView addSubview:self.titleLabel];
    //发送人
    self.senderLabel=[[UILabel alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(self.titleLabel.frame)+10,kScreenWidth-12, 20)];
    self.senderLabel.textColor=[UIColor colorWithRed:0.200 green:0.200 blue:0.200 alpha:1.00];
    self.senderLabel.font=[UIFont boldSystemFontOfSize:18];
    self.senderLabel.text=@"发送人：李浩";
    [self.notificationView addSubview:self.senderLabel];
    //内容
    self.contentLabel=[[UILabel alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(self.senderLabel.frame)+10, kScreenWidth-12, 20)];
    self.contentLabel.textColor=[UIColor colorWithRed:0.200 green:0.200 blue:0.200 alpha:1.00];
    self.contentLabel.font=[UIFont systemFontOfSize:14];
    self.contentLabel.text=@"内容";
    [self.notificationView addSubview:self.contentLabel];
    //图片
    self.notificationImageView=[[UIImageView alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(self.contentLabel.frame)+10,200, 100)];
    [self.notificationView addSubview:self.notificationImageView];
    //时间
    self.timeLabel=[[UILabel alloc] initWithFrame:CGRectMake(6, CGRectGetMaxY(self.notificationImageView.frame)+10, kScreenWidth-12, 20)];
    self.timeLabel.textColor=[UIColor colorWithRed:0.608 green:0.608 blue:0.608 alpha:1.00];
    self.timeLabel.font=[UIFont systemFontOfSize:14];
    self.timeLabel.text=@"2018-09-23 10:43";
    [self.notificationView addSubview:self.timeLabel];
    
    [self loadData];
}

- (UIImageView *)leftIcon {
    if (_leftIcon == nil) {
        UIImageView *leftBtn = [[UIImageView alloc] init];
        leftBtn.width = 15;
        leftBtn.height = 25.0;
        leftBtn.x = -3;
        leftBtn.y = 0;
        leftBtn.userInteractionEnabled = YES;
        _leftIcon = leftBtn;
    }
    return _leftIcon;
}

#pragma mark ----加载网络数据
- (void)loadData {
    [MBManager showLoading];
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
        request.requestSerializerType = ZBJSONRequestSerializer;
        request.URLString = [NSString stringWithFormat:@"%@%@",MESSAGE_DETAIL,_nocificationID];
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
    } success:^(id responseObject, apiType type) {
        //        [MBManager hideAlert];
        // 解析数据
        
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"通知详情%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1) { // 请求成功
            [MBManager hideAlert];
            NSDictionary *value=[dataDict objectForKey:@"value"];
            _titleLabel.text=[value objectForKey: @"message_title"];
            _senderLabel.text=[NSString stringWithFormat:@"发送人：%@",[value objectForKey:@"sender_username"]];
            _contentLabel.text=[value objectForKey:@"message_content"];
            _timeLabel.text=[value objectForKey:@"send_time"];
            NSString *message_images=[value objectForKey:@"message_images"];
            [_notificationImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,message_images]]];
        } else {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error) {
        DLog(@"%@",error);
        
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES) {
            DLog(@"使用了缓存");
        }
    }];
}


-(void)leftBtnDidClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
